package com.example.homeworkmutualevaluation.Controller;

import com.example.homeworkmutualevaluation.Entity.Homework;
import com.example.homeworkmutualevaluation.Entity.Student;
import com.example.homeworkmutualevaluation.Entity.Task;
import com.example.homeworkmutualevaluation.Service.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;
    @Resource
    private SCService scService;
    @Resource
    private HomeworkService homeworkService;
    @Resource
    private SubmitService submitService;
    @Resource
    private TaskService taskService;
    @Resource
    private EvaluationService evaluationService;
    @Resource
    private MessageService messageService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String phone, @RequestParam String password){
        HashMap<String, Object> response = new HashMap<>();
        if (studentService.exist(phone)) {
            Student student = studentService.find(phone, password);
            if (student == null) {
                response.put("code", 0);
                response.put("msg", "密码错误");
            } else if (student.getBannedtime().after(new Timestamp(System.currentTimeMillis()))) {
                response.put("code", 0);
                response.put("msg", "账号被封禁至[" + student.getBannedtime() + "]");
            } else {
                response.put("code", 1);
                response.put("msg", "登入成功");
                response.put("student", student);
            }
        } else {
            response.put("code", 0);
            response.put("msg", "该手机号未注册");
        }
        return response;
    }

    @PostMapping("/register")
    public Map<String, Object> register(@RequestParam String phone, @RequestParam String password){
        HashMap<String, Object> response = new HashMap<>();
        if (studentService.exist(phone)) {
            response.put("code", 0);
            response.put("msg", "该手机号已注册");
        } else if (studentService.register(phone, password)) {
            response.put("code", 1);
            response.put("msg", "注册成功");
        } else {
            response.put("code", 0);
            response.put("msg", "未知错误");
        }
        return response;
    }

    @PostMapping("/change_password")
    public Map<String, Object> changePassword(@RequestParam String phone,
                                              @RequestParam String oldPassword,
                                              @RequestParam String newPassword) {
        HashMap<String, Object> response = new HashMap<>();
        if (studentService.changePassword(phone, oldPassword, newPassword)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "密码错误");
        }
        return response;
    }

    @PostMapping("/get_info")
    public Map<String, Object> getInfo(@RequestParam String phone){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("student", studentService.findByPhone(phone));
        return response;
    }

    @PostMapping("/update_info")
    public Map<String, Object> updateInfo(String phone, String name, String sex, String birthday) {
        HashMap<String, Object> response = new HashMap<>();
        Date birthdayData;
        try {
            birthdayData = Date.valueOf(birthday);
        } catch (java.lang.IllegalArgumentException e) {
            response.put("code", 0);
            response.put("msg", "日期格式错误或日期不合法(必须为yyyy-mm-dd)");
            return response;
        }
        if (!(sex.equals("m") || sex.equals("f"))) {
            response.put("code", 0);
            response.put("msg", "性别只能是m或f");
        } else if (studentService.updateInfo(phone, name, sex, birthdayData)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        }
        return response;
    }

    @PostMapping("/select_course")
    public Map<String, Object> selectCourse(@RequestParam String sphone, @RequestParam String cno) {
        HashMap<String, Object> response = new HashMap<>();
        if (!studentService.exist(sphone)) {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        } else if (!courseService.exist(cno)) {
            response.put("code", 0);
            response.put("msg", "课程不存在");
        } else if (scService.select(sphone, cno)) {
            response.put("code", 1);
            response.put("msg", "选课成功");
        } else {
            response.put("code", 0);
            response.put("msg", "已经选了这门课了");
        }
        return response;
    }

    @PostMapping("/selected_course")
    public Map<String, Object> selectedCourse(@RequestParam String sphone){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", courseService.findAllBySphone(sphone));
        return response;
    }

    @PostMapping("/drop_course")
    public Map<String, Object> dropCourse(@RequestParam String sphone, @RequestParam String cno) {
        HashMap<String, Object> response = new HashMap<>();
        if (!studentService.exist(sphone)) {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        } else if (!courseService.exist(cno)) {
            response.put("code", 0);
            response.put("msg", "课程不存在");
        } else if (scService.delete(sphone, cno)) {
            response.put("code", 1);
            response.put("msg", "退课成功");
        } else {
            response.put("code", 0);
            response.put("msg", "该学生没有选这门课");
        }
        return response;
    }

    @PostMapping("/all_undo_homework") //返回Entity.UndoHomework列表
    public Map<String, Object> allUndoHomework(@RequestParam String sphone) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", homeworkService.findAllUndo(sphone));
        return response;
    }

    @PostMapping("/submit_homework")
    public Map<String, Object> submitHomework(@RequestParam String sphone,
                                              @RequestParam int homeworkid,
                                              @RequestParam String content
    ){
        HashMap<String, Object> response = new HashMap<>();
        Homework homework = homeworkService.findById(homeworkid);
        if (homework == null) {
            response.put("code", 0);
            response.put("msg", "作业不存在");
        } else if (homework.getDeadline().before(new Date(System.currentTimeMillis()))) {
            response.put("code", 0);
            response.put("msg", "已超过作业截止时间");
        } else if (!studentService.exist(sphone)) {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        } else {
            submitService.submitHomework(sphone, homeworkid, content);
            response.put("code", 1);
            response.put("msg", "作业提交成功");
        }
        return response;
    }

    @PostMapping("/get_all_submitted")
    public Map<String, Object> getAllSubmitted(@RequestParam String sphone) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", submitService.findAllBySphone(sphone));
        return response;
    }

    @PostMapping("/tasks_need_to_do")
    public Map<String, Object> tasksNeedToDo(@RequestParam String sphone, @RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", taskService.findTasksNeedToDo(sphone, homeworkid));
        return response;
    }

    @PostMapping("/tasks_have_done")
    public Map<String, Object> tasksHaveDone(@RequestParam String sphone, @RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", taskService.findTasksHasDone(sphone, homeworkid));
        return response;
    }

    @PostMapping("/get_evaluate_detail")
    public Map<String, Object> getEvaluateDetail(@RequestParam int taskid) {
        HashMap<String, Object> response = new HashMap<>();
        Task task = taskService.findById(taskid);
        if (task != null) {
            response.put("code", 1);
            HashMap<String, Object> data = new HashMap<>();
            data.put("homework", homeworkService.findById(task.getHomeworkid()));
            data.put("submit_content", submitService.getContentByTaskid(taskid));
            response.put("data", data);
        } else {
            response.put("code", 0);
            response.put("msg", "批改任务不存在");
        }
        return response;
    }

    @PostMapping("/evaluate")
    public Map<String, Object> evaluate(@RequestParam int taskid,
                                        @RequestParam int score,
                                        @RequestParam String comment) {
        HashMap<String, Object> response = new HashMap<>();
        Task task = taskService.findById(taskid);
        if (task == null) {
            response.put("code", 0);
            response.put("msg", "批改任务不存在");
        } else {
            Homework homework = homeworkService.findById(task.getHomeworkid());
            if (homework.getScoretime().before(new Date(System.currentTimeMillis()))) {
                response.put("code", 0);
                response.put("msg", "已超过互评截止时间");
            } else {
                evaluationService.submitEvaluation(taskid, score, comment);
                response.put("code", 1);
                response.put("msg", "评价成功");
            }
        }
        return response;
    }

    @PostMapping("/require_task")
    public Map<String, Object> requireTask(@RequestParam String sphone, @RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        if (taskService.existPendingTask(sphone, homeworkid)) {
            response.put("code", 0);
            response.put("msg", "存在未完成的批改任务");
        } else {
            int taskid = taskService.requireTask(sphone, homeworkid);
            if (taskid == -1) {
                response.put("code", 0);
                response.put("msg", "没有可批改的作业了");
            } else {
                response.put("code", 1);
                response.put("taskid", taskid);
            }
        }
        return response;
    }

    @PostMapping("/all_message")
    public Map<String, Object> allMessage(String sphone) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", messageService.getAllMsg(sphone));
        return response;
    }

    @PostMapping("/on_read_message")
    public Map<String, Object> onReadMessage(String msgid) {
        HashMap<String, Object> response = new HashMap<>();
        if (messageService.onRead(msgid)) {
            response.put("code", 1);
        } else {
            response.put("code", 0);
            response.put("msg", "无效msgid");
        }
        return response;
    }
}
